package com.aier.classes.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.classes.common.ClassesFinalPageSize;
import com.aier.classes.entity.ClassParent;
import com.aier.classes.entity.ClassStudent;
import com.aier.classes.entity.ClassTeacher;
import com.aier.classes.service.IClassUserService;
import com.aier.classes.service.IInviteRequestService;
import com.aier.common.common.Constant;
import com.aier.common.dao.ICommonDAO;

@Scope("prototype")
@Component("classesMemberManageAction")
public class ClassesMemberManageAction extends ClassesBaseAction{

    /**
     * 班级管理-班级成员管理
     */
    private static final long serialVersionUID = 1L;

    @Resource(name = "classUserService")
    private IClassUserService classUserService;    
    @Resource(name = "inviteRequestService")
    private IInviteRequestService inviteRequestService; 
    
    private int s;                //成功:1 ,失败:0
    private String error_info;    //错误提示信息
        
    private List<ClassTeacher> classTeacherList;    //班级教师列表        
    private List<ClassStudent> classStudentList;    //班级学生列表        
    private List<ClassParent> classParentList;      //班级家长列表
    private Long cid;             //班级id
    private Long ctid;            //班级教师id
    private Long csid;            //班级学生id
    private Long cpid;            //班级家长id
    private Short isadmin_class;  //是否班级管理员
    
    private int page;             //页数
    private int page_size;        //每页的记录数
    private int page_count;       //总页数
    private int count;            //总数
    
    private Integer method;       //学校是何种付费方式
    
    private int pcount;           //班级家长数量
    private String csidStr;       //多个csid,以，号分割
    private String name;          //姓名
    private String sex;           //性别（男，女）
    private Long tid;             //发送邀请的教师tid
    private String invitePar_message;     //家长邀请的字符串 "relation,pname,phone@XXX"
    private Long uid;             //用户id
    private String cpid_delete;     //需要删除的班级家长id(多个以，号分隔)
    private String updatePar_message;     //修改家长的字符串"cpid,relation,pname,phone@XXX"
    
 //=============================================================================================    
    @Override
    public String execute(){
        return SUCCESS;
    }
    
    /**
     * 查询班级教师
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchClassTeacherList(){
        s = 1;    
        Map<String,Object> map = classUserService.searchClassTeacherList(page, ClassesFinalPageSize.CLASSES_TEACHER_LIST_PAGESIZE,cid);
        if(null==map){
            s = 0;
        }
        classTeacherList = (List<ClassTeacher>) map.get(ICommonDAO.OBJ_LIST);
        if (null == classTeacherList) {
            s = 0;
        }
        page = (Integer) map.get(ICommonDAO.PAGE);
        page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
        page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
        count = (Integer) map.get(ICommonDAO.COUNT);
        return SUCCESS;
    }
    
    /**
     * 删除班级教师
     * @return
     */
    public String deleteClassTeacher(){
        s = classUserService.deleteClassTeacher(ctid);
        return SUCCESS;
    }
    
    /**
     * 修改班级教师 班级管理权限
     */
    public String updateClassTeacherAdmin(){
        s = classUserService.updateClassTeacherState(ctid, isadmin_class);
        return SUCCESS;
    }

//=============================================================================================

    /**
     * 查询班级学生
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchClassStudentList(){
        s = 1;    
        Map<String,Object> map = classUserService.searchClassStudentList(page, ClassesFinalPageSize.CLASSES_STUDENT_LIST_PAGESIZE,cid);
        if(null==map){
            s = 0;
        }
        classStudentList = (List<ClassStudent>) map.get(ICommonDAO.OBJ_LIST);
        if (null == classStudentList) {
            s = 0;
        }
        page = (Integer) map.get(ICommonDAO.PAGE);
        page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
        page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
        count = (Integer) map.get(ICommonDAO.COUNT);
        return SUCCESS;
    }
    
    /**
     * 删除班级学生
     * @return
     */
    public String deleteClassStudent(){
        s = classUserService.deleteClassStudent(csid);
        return SUCCESS;
    }

//=============================================================================================
    
    /**
     * 查询班级家长
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchClassParentList(){
        s = 1;    
        Map<String,Object> map = classUserService.searchClassParentList(page, ClassesFinalPageSize.CLASSES_PARENT_LIST_PAGESIZE,cid);
        if(null==map){
            s = 0;
        }
        classParentList = (List<ClassParent>) map.get(ICommonDAO.OBJ_LIST);
        if (null == classParentList) {
            s = 0;
        }
        
        page = (Integer) map.get(ICommonDAO.PAGE);
        page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
        page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
        count = (Integer) map.get(ICommonDAO.COUNT);
        return SUCCESS;
    }
    
    /**
     * 查询学校的购买服务的方式
     * @return
     */
    public String searchSchoolBuyMethod() {
        method = classUserService.searchSchoolBuyMethodByCid(cid);
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 删除班级家长
     * @return
     */
    public String deleteClassParent(){
        s = classUserService.deleteClassParent(cpid);
        return SUCCESS;
    }

//=============================================================================================
    
    // 新版
    /**
     * 查询班级学生家长列表(app端)
     * @return
     */
    public String searchClassParentListApp(){
        s = 1;    
        classStudentList = classUserService.searchClassParentApp(cid);
        return SUCCESS;
    }
    
    /**
     * 查询班级教师列表(app端)
     * @return
     */
    public String searchClassTeacherListApp(){
        s = 1;    
        classTeacherList = classUserService.searchClassTeacherListApp(cid);
        return SUCCESS;
    }
    
    /**
     * 成员管理-查询班级的学生和家长
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchClassStuParList() {
        s = 1;
        if (page <= 0) {
            page = 1;
        }
        if (page_size <= 0) {
            page_size = Constant.num_10;
        }
        Map<String,Object> map = classUserService.searchClassStudent(page, page_size,cid);
        classStudentList = (List<ClassStudent>) map.get(ICommonDAO.OBJ_LIST);
        
        page = (Integer) map.get(ICommonDAO.PAGE);
        page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
        page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
        count = (Integer) map.get(ICommonDAO.COUNT);
        
        pcount = classUserService.searchClassParentCount(cid);
        return SUCCESS;
    }
    
    /**
     * 删除班级学生（多选）
     * @return
     */
    public String deleteClassStudentN(){
        for (String str : csidStr.split(",")) {
            s = classUserService.deleteClassStudent(Long.valueOf(str));
        }
        return SUCCESS;
    }
    
    /**
     * 添加宝宝、家长
     */
    public String addClassStudent(){
        s = 0;
        Long csid = classUserService.insertClassStudent(cid, name,sex);
        if (csid != 0) {
            if (invitePar_message != null && !"".equals(invitePar_message)) {
                String mes = "";
                for (String in : invitePar_message.split("@")) {
                    mes = mes + csid + "," + in + "@";
                }
                s = inviteRequestService.insertParentList(tid, cid,mes);
            } else {
                s = 1;
            }
        }
        
        return SUCCESS;
    }
    
    /**
     * 修改宝宝、家长信息
     */
    public String updateClassStudent(){
        // 修改学生姓名、性别
        if (name != null) {
            classUserService.updateClassStudentName(csid, name);
        }
        if (sex != null) {
            classUserService.updateClassStudentSex(uid, sex);
        }
        // 删除家长信息
        if (cpid_delete != null && !"".equals(cpid_delete)) {
            for (String str : cpid_delete.split(",")) {
                classUserService.deleteClassParent(Long.valueOf(str));
            }
        }
        // 新增家长信息
        if (invitePar_message != null && !"".equals(invitePar_message)) {
            String mes = "";
            for (String in : invitePar_message.split("@")) {
                mes = mes + csid + "," + in + "@";
            }
            inviteRequestService.insertParentList(tid, cid,mes);
        }
        // 修改家长信息
        if (updatePar_message != null && !"".equals(updatePar_message)) {
            inviteRequestService.updateParentList(tid, cid, updatePar_message);
        }
        s = 1;
        return SUCCESS;
    }
    
    
    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPage_size() {
        return page_size;
    }

    public void setPage_size(int page_size) {
        this.page_size = page_size;
    }

    public int getPage_count() {
        return page_count;
    }

    public void setPage_count(int page_count) {
        this.page_count = page_count;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getS() {
        return s;
    }

    public String getError_info() {
        return error_info;
    }

    public List<ClassTeacher> getClassTeacherList() {
        return classTeacherList;
    }

    public List<ClassStudent> getClassStudentList() {
        return classStudentList;
    }

    public List<ClassParent> getClassParentList() {
        return classParentList;
    }

    public void setClassUserService(IClassUserService classUserService) {
        this.classUserService = classUserService;
    }

    public void setCid(Long cid) {
        this.cid = cid;
    }

    public void setCtid(Long ctid) {
        this.ctid = ctid;
    }

    public void setCsid(Long csid) {
        this.csid = csid;
    }

    public void setCpid(Long cpid) {
        this.cpid = cpid;
    }

    public void setIsadmin_class(Short isadmin_class) {
        this.isadmin_class = isadmin_class;
    }

    public Long getCid() {
        return cid;
    }

    public Integer getMethod() {
        return method;
    }

    public int getPcount() {
        return pcount;
    }

    public void setCsidStr(String csidStr) {
        this.csidStr = csidStr;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public void setInviteRequestService(IInviteRequestService inviteRequestService) {
        this.inviteRequestService = inviteRequestService;
    }

    public void setUid(Long uid) {
        this.uid = uid;
    }

    public void setCpid_delete(String cpid_delete) {
        this.cpid_delete = cpid_delete;
    }

    public void setUpdatePar_message(String updatePar_message) {
        this.updatePar_message = updatePar_message;
    }

    public void setInvitePar_message(String invitePar_message) {
        this.invitePar_message = invitePar_message;
    }

    public void setTid(Long tid) {
        this.tid = tid;
    }

    
    
}
